package com.example.voicepad.ui;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.voicepad.data.NoteDaoImpl;
import com.example.voicepad.model.Note;
import com.workingteams.mobile.voicepad.R;

public class ViewNotesActivity extends Activity {
	
	private List<Note> notes;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_notes_activity);

		this.setNotes(NoteDaoImpl.getInstance(this).selectAll());

		ListView notesList = (ListView) findViewById(R.id.notes_list);
		
		notesList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                
            	ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>)((ListView)parent).getAdapter();
            	Note note = arrayAdapter.getItem(position);

            	NoteDaoImpl.getInstance(ViewNotesActivity.this).delete(note.getId());
            	getNotes().remove(note);
            	
            	arrayAdapter.notifyDataSetChanged();
            	
            }
        });

		TextView notesStatus = (TextView) findViewById(R.id.notes_status);

		if (!this.getNotes().isEmpty()) {
			notesList.setVisibility(View.VISIBLE);
			notesStatus.setVisibility(View.INVISIBLE);

			notesList.setAdapter(new ArrayAdapter<Note>(this, android.R.layout.simple_list_item_1, this.getNotes()));
		} else {
			notesList.setVisibility(View.INVISIBLE);
			notesStatus.setVisibility(View.VISIBLE);
			
			notesStatus.setText("No hay notas cargadas!");
		}

	}

	public List<Note> getNotes() {
		return notes;
	}

	public void setNotes(List<Note> notes) {
		this.notes = notes;
	}

}
